<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/13 17:38
 */

namespace App\Transformers;

use App\Models\DrugDictionary;
use League\Fractal\TransformerAbstract;

class MedicalDrugTransformer extends TransformerAbstract
{
//    protected $availableIncludes = ['detail'];

    /**
     * @var \App\Project\FieldTranslation\FieldTranslation
     */
    protected $field;

    public function __construct()
    {
        $this->field = app('field')->get('drug');
    }

    /**
     * @param \App\Models\DrugDictionary $drug
     * @return array
     */
    public function transform(DrugDictionary $drug)
    {
        return [
            'id'   => $drug->id,
            'name' => $drug->name,
        ];
    }

    /**
     * 返回信息中加入药品的详细信息
     * @param \App\Models\DrugDictionary $drug
     * @return \League\Fractal\Resource\Item
     */
    public function includeDetail(DrugDictionary $drug)
    {

        return $this->item($drug, function (DrugDictionary $drug) {

            return [
                //'doctor'       => $drug->doctor()->getQuery()->select(['id', 'name'])->first(),
                'is_recommend' => [
                    'key'   => $drug->is_recommend,
                    'value' => $this->field['is_recommend'][$drug->is_recommend]
                ],
                'is_insulin'   => [
                    'key'   => $drug->is_insulin,
                    'value' => $this->field['is_insulin'][$drug->is_insulin]
                ],
//                'type'         => [
//                    'key'   => $drug->type,
//                    'value' => $this->field['type'][$drug->type]
//                ],
//                'group'        => [
//                    'key'   => $drug->group,
//                    'value' => $this->field['group'][$drug->group]
//                ],
                'pic'          => $drug->pic,
                'sort'         => $drug->sort,
                'remark'       => $drug->remark,
                'created_at'   => $drug->created_at->toDateTimeString(),
                'updated_at'   => $drug->updated_at->toDateTimeString(),
            ];
        });
    }
}